﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewAthleticDataById : System.Web.UI.Page
    {
        // Rows property to hold the Rows in the ViewState
        protected int Rows
        {
            get
            {
                return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
            }
            set
            {
                ViewState["Rows"] = value;
            }
        }

        // Columns property to hold the Columns in the ViewState
        protected int Columns
        {
            get
            {
                return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
            }
            set
            {
                ViewState["Columns"] = value;
            }
        }

        public List<AthleticDataDTO> AllAthleticData
        {
            get
            {
                if (Session["AllAthleticData"] == null)
                {
                    return athleticAL.GetAllAthleticData();
                }
                else
                {
                    return (List<AthleticDataDTO>)Session["AllAthleticData"];
                }
            }
            set
            {
                Session["AllAthleticData"] = value;
            }
        }

        protected int currentPageNumber = 1;
        List<AthleticDataDTO> oneAthleticData;
        string queryString;
        AthleticAL athleticAL;
        SportsUtil sportsUtil;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();

                string firstPage = "";
                string secondPage = "";
                string afterRemovedPlusSign = "";
                Guid selectedCompetitionId = Guid.Empty;
                string selectedYear = "";
                sportsUtil = new SportsUtil();


                string[] arguments = new string[5];
                arguments = queryString.Split('&');

                foreach (string argument in arguments)
                {
                    if (argument.Contains("+"))
                    {
                        afterRemovedPlusSign = argument.Replace("+", " ");
                    }
                    else
                    {
                        afterRemovedPlusSign = argument;
                    }



                    if (argument.Contains("SelectedAthleticEventId"))
                    {

                        selectedCompetitionId = new Guid(afterRemovedPlusSign.Substring((afterRemovedPlusSign.IndexOf('=') + 1)));
                    }

                    if (argument.Contains("SelectedYear"))
                    {

                        selectedYear = sportsUtil.decryptQueryStrings(afterRemovedPlusSign.Substring(afterRemovedPlusSign.IndexOf('=') + 1));
                    }
                }

                CreateDynamicTable(selectedCompetitionId, selectedYear);

            }
        }

        private void CreateDynamicTable(Guid competnDataId, string year)
        {
            PlaceHolder5.Controls.Clear();

            StudentAL studentAL = new StudentAL();
            athleticAL = new AthleticAL();
            List<AthleticDataDTO> athleticDataDTO = new List<AthleticDataDTO>();
            if (!string.IsNullOrEmpty(year))
            {
                athleticDataDTO = athleticAL.GetAthleticDataByYearByLevelByAgeGruopByEvent( Convert.ToInt32(year),"",athleticAL.GetAthleticEventById(competnDataId)[0].AgeDivision,athleticAL.GetAthleticEventById(competnDataId)[0].EventCategory);
            }
            else
              {
                  athleticDataDTO = athleticAL.GetAthleticDataByYearByLevelByAgeGruopByEvent(0, "", athleticAL.GetAthleticEventById(competnDataId)[0].AgeDivision, athleticAL.GetAthleticEventById(competnDataId)[0].EventCategory);
             }

            //List<AthleticDataDTO> athleticDTO = athleticAL.GetCompetitionById(competnDataDTO[0].CompetionId);


            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder5.Controls.Add(tbl);


            Label lblCompetitionYear = new Label();
            lblCompetitionYear.Text = "Year";

            Label lblCompetitionDivision = new Label();
            lblCompetitionDivision.Text = "Division";

            Label lblCompetitionEvent = new Label();
            lblCompetitionEvent.Text = "Event";


            List<Label> allHeadeLabels = new List<Label>();

            allHeadeLabels.Add(lblCompetitionYear);
            allHeadeLabels.Add(lblCompetitionDivision);
            allHeadeLabels.Add(lblCompetitionEvent);

            int i = 0;
            foreach (Label lblHeader in allHeadeLabels)
            {


                TableRow trHeader = new TableRow();
                trHeader.Width = new Unit("50%");

                TableCell tcHeader = new TableCell();
                TableCell tcHeaderValue = new TableCell();

                Label lblValue = new Label();
                if (i % 2 == 0)
                {
                    tcHeader.BackColor = System.Drawing.Color.Blue;
                    tcHeader.ForeColor = System.Drawing.Color.White;

                    tcHeaderValue.BackColor = System.Drawing.Color.Blue;
                    tcHeaderValue.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcHeader.BackColor = System.Drawing.Color.White;
                    tcHeader.ForeColor = System.Drawing.Color.Blue;

                    tcHeaderValue.BackColor = System.Drawing.Color.White;
                    tcHeaderValue.ForeColor = System.Drawing.Color.Blue;
                }

                if (lblHeader.Text == "Year")
                {
                    lblValue.Text = athleticDataDTO[0].Year.ToString();
                }

                else if (lblHeader.Text == "Level Name")
                {
                    lblValue.Text = athleticDataDTO[0].LevelName;
                }

                else if (lblHeader.Text == "Age Division")
                {
                    lblValue.Text = athleticDataDTO[0].AgeDivision;
                }

                else if (lblHeader.Text == "Event Category")
                {
                    lblValue.Text = athleticDataDTO[0].EventCategory;
                }
                tcHeader.Controls.Add(lblHeader);
                tcHeaderValue.Controls.Add(lblValue);
                trHeader.Cells.Add(tcHeader);
                trHeader.Cells.Add(tcHeaderValue);


                tbl.Rows.Add(trHeader);
                i++;
            }

            int tblRows = athleticDataDTO.Count;


            // Now iterate through the table and add your controls 
            for (int j = 0; j < tblRows; j++)
            {
                List<StudentDTO> studentDTO = studentAL.GetStudentById(athleticDataDTO[j].StudentId);
                TableRow trBody = new TableRow();
                trBody.Width = new Unit("50%");

                TableCell tcBodyPlace = new TableCell();
                TableCell tcBodyStudentName = new TableCell();

                Label lblPlace = new Label();
                HyperLink hlStudentName = new HyperLink();
                if (i % 2 == 0)
                {
                    tcBodyPlace.BackColor = System.Drawing.Color.DarkSlateBlue;
                    tcBodyPlace.ForeColor = System.Drawing.Color.White;

                    tcBodyStudentName.BackColor = System.Drawing.Color.DarkSlateBlue;
                    tcBodyStudentName.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcBodyPlace.BackColor = System.Drawing.Color.White;
                    tcBodyPlace.ForeColor = System.Drawing.Color.DarkSlateBlue;

                    tcBodyStudentName.BackColor = System.Drawing.Color.White;
                    tcBodyStudentName.ForeColor = System.Drawing.Color.DarkSlateBlue;
                }

                lblPlace.Text = athleticDataDTO[j].Place;
                hlStudentName.Text = studentDTO[0].FullName;
                hlStudentName.NavigateUrl = "viewAllAthleticPerformanceOfStudent.aspx?" + studentDTO[0].StudentId;


                tcBodyPlace.Controls.Add(lblPlace);
                tcBodyStudentName.Controls.Add(hlStudentName);

                trBody.Cells.Add(tcBodyPlace);
                trBody.Cells.Add(tcBodyStudentName);


                tbl.Rows.Add(trBody);
                i++;
            }

            // Add the TableRow to the Table

            //tbl.Rows.Add(trBody);
            // }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }
 
    }
}